#include "SignUpWidget.h"
#include "ui_SignUpWidget.h"
#include "Login-ly.h"
#include <QPainter>
#include <QIcon>
#include <QRegularExpression>
#include <QRegularExpressionValidator>
#include <QMessageBox>
#include <QDebug>
#include <QSqlError>
#include <QMenu>
#include <QRegExp>
#include <QPixmap>
#include <QSystemTrayIcon>
#include <QMenu>

SignUpWidget::SignUpWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SignUpWidget)
{
    setWindowFlags(Qt::FramelessWindowHint | Qt::Window);


    setAttribute(Qt::WA_TranslucentBackground);

    setWindowIcon(QIcon(":/UIsources/logo2.png"));



    ui->setupUi(this);
    //QPixmap qpixmap=QPixmap(":/Image/1.png");
    //i->photo->setPixmap(qpixmap.scaled(ui->photo->size(),Qt::KeepAspectRatio,Qt::SmoothTransformation));
    //ui->photo->setPixmap(qpixmap);


    //添加mysql
    db=QSqlDatabase::addDatabase("QMYSQL");

    db.setHostName("192.168.76.128");
    db.setUserName("root");
    db.setPassword("123456");
    db.setDatabaseName("test268");
    db.setPort(3306);


//    if(!db.open())
//    {
//        qDebug()<<db.lastError()<<endl;
//        //return ;
//    }
    qDebug()<<"打开成功"<<endl;



    //setWindowIcon(QIcon(":/Image/register.png"));
    setWindowTitle("注册");
    ui->account->setFocus();
    ui->account->setPlaceholderText("xxx.@xxx");
    ui->account->setStyleSheet("QLineEdit{font-size:10px}");


    ui->password->setPlaceholderText("8-16大小写字母+数字+特殊字符");
    ui->password->setStyleSheet("QLineEdit{font-size:6px}");

    ui->first_check->setText("显示");
    ui->second_check->setText("显示");
    ui->password->setEchoMode(QLineEdit::Password);
    ui->configure_password->setPlaceholderText("再次确认密码");
    ui->configure_password->setStyleSheet("QLineEdit{font-size:6px}");
    ui->configure_password->setEchoMode(QLineEdit::Password);
    ui->pushButton->setText(QStringLiteral("注册"));



    //注册权限设置
    ui->limits->clear();
    //管理员
    QListWidgetItem *_manager=new QListWidgetItem;
    _manager->setText(QStringLiteral("管理员"));
    _manager->setFlags(_manager->flags() | Qt::ItemIsUserCheckable); // 设置为可勾选
    _manager->setCheckState(Qt::Unchecked); // 初始为未勾选状态
    ui->limits->addItem(_manager);
    //老师
    QListWidgetItem *_teacher=new QListWidgetItem;
    _teacher->setText(QStringLiteral("老师"));
    _teacher->setFlags(_teacher->flags() | Qt::ItemIsUserCheckable);
    _teacher->setCheckState(Qt::Unchecked);
    ui->limits->addItem(_teacher);
    //学生
    QListWidgetItem *_student=new QListWidgetItem;
    _student->setText(QStringLiteral("学生"));
    _student->setFlags(_student->flags() | Qt::ItemIsUserCheckable);
    _student->setCheckState(Qt::Unchecked);
    ui->limits->addItem(_student);
    ui->limits->setStyleSheet(
    "QListWidget::item:hover {"
    "background-color: lightblue;"   // 悬停时背景颜色
    "color: white;"                  // 悬停时文字颜色
    "}");

    ui->pushButton->setStyleSheet(
    "QPushButton:hover {"
    "background-color: lightblue;"   // 悬停时背景颜色
    "color: white;"                  // 悬停时文字颜色
    "}");

    ui->first_check->setStyleSheet(
    "QCheckBox:hover {"
    "background-color: lightblue;"   // 悬停时背景颜色
    "color: white;"                  // 悬停时文字颜色
    "}");

    ui->second_check->setStyleSheet(
    "QCheckBox:hover {"
    "background-color: lightblue;"   // 悬停时背景颜色
    "color: white;"                  // 悬停时文字颜色
    "}");

    ui->account->setStyleSheet(
    "QLineEdit:hover {"
    "background-color: lightblue;"   // 悬停时背景颜色
    "color: white;"                  // 悬停时文字颜色
    "}");

    ui->password->setStyleSheet(
    "QLineEdit:hover {"
    "background-color: lightblue;"   // 悬停时背景颜色
    "color: white;"                  // 悬停时文字颜色
    "}");

    ui->configure_password->setStyleSheet(
    "QLineEdit:hover {"
    "background-color: lightblue;"   // 悬停时背景颜色
    "color: white;"                  // 悬停时文字颜色
    "}");



    connect(ui->pushButton,&QPushButton::clicked,this,&SignUpWidget::Identify_register);

    connect(ui->first_check,&QCheckBox::clicked,this,&SignUpWidget::Show_Passwd);
    connect(ui->second_check,&QCheckBox::clicked,this,&SignUpWidget::Show_Passwd);

    connect(ui->first_check,&QCheckBox::clicked,this,&SignUpWidget::Hide_Passwd);
    connect(ui->second_check,&QCheckBox::clicked,this,&SignUpWidget::Hide_Passwd);
    connect(ui->limits,&QListWidget::itemChanged,this,&SignUpWidget::Only_select_one_limts);

    connect(ui->smail,&QPushButton::clicked, this,&SignUpWidget::minimizeWindow);

}

bool SignUpWidget::select_identify_()
{
    QString identify;
    for(int i=0;i<ui->limits->count();++i)
    {
        QListWidgetItem *item = ui->limits->item(i);
        if (item->checkState() == Qt::Checked)
        {
            identify=item->text();
        }
    }
    if(identify.length()==0)
    {
        QMessageBox::warning(this,"警告","未选择注册身份");
        return false;
    }
    return true;
}

bool SignUpWidget::account_input_()
{
    QString account_=ui->account->text();
    QString first_passwd=ui->password->text();
    QString second_passwd=ui->configure_password->text();

    if(account_.length()==0)
    {
        QMessageBox::warning(this,"警告","输入账号为空");
        ui->account->setFocus();
        return false;
    }
    return true;
}

bool SignUpWidget::passws_input_()
{
    if(ui->password->text().length()==0)
    {
        QMessageBox::warning(this,"警告","未输入密码");
        ui->password->setFocus();
        return false;
    }
    return true;
}

bool SignUpWidget::second_passed_input_()
{
    if(ui->configure_password->text().length()==0)
    {
        QMessageBox::warning(this,"警告","请再次输入密码核对");
        ui->configure_password->setFocus();
        return false;
    }
    return true;
}

bool SignUpWidget::same_passwd()
{
    QString first_passwd=ui->password->text();
    QString second_passwd=ui->configure_password->text();
    if(first_passwd!=second_passwd)
    {
        QMessageBox::warning(this, "错误", "两次输入密码不一致！");
        return false;
    }
    return true;
}

bool SignUpWidget::Repetiton()
{
    QSqlQuery query(db);
    QString account_=ui->account->text();
    qDebug()<<account_<<endl;
    QString sql="select account from info where account = '"+account_+"'";
    qDebug()<<sql<<endl;
    if(query.exec(sql))
    {
        if(query.next())
        {

            qDebug()<<query.value(0).toString()<<endl;
            QString  result= query.value(0).toString();
            if(result.length()!=0)
            {
                return false;
            }else {
                return true;
            }

        }


    }

}
void SignUpWidget::Only_select_one_limts(QListWidgetItem *changedItem)
{
//    ui->limits->item(0)->setCheckState(Qt::Checked);

    if(changedItem->checkState()==Qt::Checked)
    {
        for(int i=0;i<ui->limits->count();++i)
        {
            QListWidgetItem *item = ui->limits->item(i);
            if (item->checkState() == Qt::Checked && item != changedItem)
            {
                item->setCheckState(Qt::Unchecked);
            }
        }
    }
}

void SignUpWidget::Identify_register()
{

    QString identify;
    for(int i=0;i<ui->limits->count();++i)
    {
        QListWidgetItem *item = ui->limits->item(i);
        if (item->checkState() == Qt::Checked)
        {
            identify=item->text();
        }
    }

    if(select_identify_())
    {
        if(account_input_())
        {
            if(passws_input_())
            {
                 if(second_passed_input_())
                 {
                     if(same_passwd())
                     {
                         if(identify=="管理员")
                         {

                             QMessageBox::information(this,"信息","已提交管理员身份注册申请");
                             Register();
                         }else if (identify=="老师")
                         {

                             QMessageBox::information(this,"信息","已提交教师身份注册申请");
                             Register();
                         }else if(identify=="学生")
                         {

                             QMessageBox::information(this,"信息","已提交学生身份注册申请");
                             Register();
                         }
                     }
                 }

            }
        }
    }


}







SignUpWidget::~SignUpWidget()
{
    delete ui;
}


void SignUpWidget::paintEvent(QPaintEvent *e)
{
    QPainter paint(this);
    QPixmap image(":/Image/background.png");
    paint.drawPixmap(0,0,this->width(),this->height(),image);
}

void SignUpWidget::Register()
{

    QString account_=ui->account->text();
    QString first_passwd=ui->password->text();
    QString second_passwd=ui->configure_password->text();

    if(Repetiton())
    {
        QSqlQuery query;
        query.prepare("insert into info(account,passwd) values(:account,:passwd);");
        query.bindValue(":account",ui->account->text());
        query.bindValue(":passwd",ui->password->text());
        if(query.exec())
        {
            for(int i=0;i<ui->limits->count();++i)
            {
                QListWidgetItem *item = ui->limits->item(i);
                if (item->checkState() == Qt::Checked)
                {
                    item->setCheckState(Qt::Unchecked);
                }
            }
             ui->account->clear();
             ui->password->clear();
             ui->configure_password->clear();
             this->hide();
             Login_ly * s1=new Login_ly;
             s1->show();
         }
    }else
    {
        QMessageBox::information(this,"错误","该账号已被注册");
    }


}

void SignUpWidget::Show_Passwd(bool checked)
{
    if(checked && sender()==ui->first_check)
    {
        ui->password->setEchoMode(QLineEdit::Normal);
    }

    if(checked && sender()==ui->second_check)
    {
        ui->configure_password->setEchoMode(QLineEdit::Normal);
    }
}

void SignUpWidget::Hide_Passwd(bool checked)
{
    if(!checked && sender()==ui->first_check)
    {
        ui->password->setEchoMode(QLineEdit::Password);
    }

    if(!checked && sender()==ui->second_check)
    {
        ui->configure_password->setEchoMode(QLineEdit::Password);
    }
}



void SignUpWidget::on_account_editingFinished()
{
    QRegExp rx("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$");
    if(!rx.exactMatch(ui->account->text()) && ui->account->text().length()!=0)//匹配邮箱格式
     {
        QMessageBox::information(this,"提示","账号格式错误");
        ui->pushButton->setEnabled(rx.exactMatch(ui->account->text()));

     }else if(ui->account->text().length()!=0)
     {
        ui->pushButton->setEnabled(true);

     }
}

void SignUpWidget::on_password_editingFinished()
{
    QRegExp rx("^(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()_+{}:\"<>?~]).{8,16}$");
    if(!rx.exactMatch(ui->password->text()) && ui->password->text().length()!=0)
     {
         QMessageBox::information(this,"提示","密码格式错误");
         ui->pushButton->setEnabled(rx.exactMatch(ui->account->text()));
      }else if(ui->password->text().length()!=0)
      {
         ui->pushButton->setEnabled(true);
      }
}


void SignUpWidget::on_close_button_clicked()
{
    this->hide();
    Login_ly * s2=new Login_ly;
    s2->show();
}

void SignUpWidget::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)// 检查是否是左键
    {
        m_dragging = true;// 设置拖拽标志
        m_dragPosition = event->globalPos() - frameGeometry().topLeft();// 记录鼠标点击时相对于窗口左上角的位置
        event->accept();// 接受事件
    }
}

void SignUpWidget::mouseMoveEvent(QMouseEvent *event)
{
    if (m_dragging && (event->buttons() & Qt::LeftButton)) // 检查是否正在拖拽且鼠标左键按下
    {
        move(event->globalPos() - m_dragPosition); // 移动窗口到新的位置
        event->accept();// 接受事件
    }
}

void SignUpWidget::mouseReleaseEvent(QMouseEvent *event)
{
     m_dragging = false;// 拖拽结束
}
